<template>
	<div class="system-dept-dialog-container">
		<el-dialog :title="state.dialog.title" draggable :close-on-click-modal="false" v-model="state.dialog.isShowDialog" width="769px">
			<!-- <template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ state.dialog.title }} </span>
				</div>
			</template> -->
			<el-form ref="orgDialogFormRef" :model="state.orgForm" size="default" label-width="auto">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="上级机构">
							<el-cascader
								:options="props.orgData"
								:props="{ checkStrictly: true, emitPath: false, value: 'id', label: 'name' }"
								placeholder="请选择上级机构"
								clearable
								class="w100"
								v-model="state.orgForm.pid"
							>
								<template #default="{ node, data }">
									<span>{{ data.name }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="机构名称">
							<el-input v-model="state.orgForm.name" placeholder="请输入机构名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="机构编码">
							<el-input v-model="state.orgForm.code" placeholder="请输入机构编码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="机构负责人">
							<el-input v-model="state.orgForm.manager" placeholder="请输入机构负责人" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号">
							<el-input v-model="state.orgForm.phone" placeholder="请输入手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="邮箱">
							<el-input v-model="state.orgForm.email" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number v-model="state.orgForm.orderNo" :min="0" :max="999" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态">
							<el-radio-group v-model="state.orgForm.status">
								<el-radio :label="0">启用</el-radio>
								<el-radio :label="1">不启用</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col> -->
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.orgForm.remarks" type="textarea" placeholder="请输入机构描述" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemOrgDialog">
import { reactive, ref } from 'vue';
import { useOrgApi } from '/@/api/org';
import { ElMessage } from 'element-plus';

// 定义子组件向父组件传值/事件
const emits = defineEmits(['refresh']);
//const emits = defineEmits(['handleQuery']);
const props = defineProps({
	title:String,
	orgData:Array<OrgItem>,
});

// 定义变量内容
const orgDialogFormRef = ref();
const state = reactive({
	orgForm: {} as OrgForm,
	//deptData: [] as DeptTreeType[], // 部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: OrgForm) => {
	//console.log('type:'+type+',row:'+JSON.stringify(row));
	if (type === 'edit') {
		//console.log('编辑');
		// 模拟数据，实际请走接口
		//row.menuType = 'menu';
		//row.menuSort = Math.floor(Math.random() * 100);
		state.orgForm = row; //JSON.parse(JSON.stringify(row));
		state.dialog.title = '修改机构';
		state.dialog.submitTxt = '修 改';
	} else {
		//console.log('添加');
		state.dialog.title = '新增机构';
		state.dialog.submitTxt = '新 增';
		state.orgForm = row;
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	menuDialogFormRef.value.resetFields();
		// });
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery', true);
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};
// // 提交
// const onSubmit = () => {
// 	closeDialog();
// 	emit('refresh');
// 	// if (state.dialog.type === 'add') { }
// };
// 提交
const onSubmit = () => {
	orgDialogFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		if (state.orgForm.id != undefined && state.orgForm.id > 0) {
			useOrgApi().editAdminOrg(state.orgForm).then(async (res: AjaxResult) => {
				if (res.success == true) {
					ElMessage.success('更新成功');
					emits('handleQuery');
					closeDialog();
				}
				else {
					ElMessage.warning('更新失败：' + res.message);
				}
			});
		} else {
			useOrgApi().addAdminOrg(state.orgForm).then(async (res: AjaxResult) => {
				if (res.success == true) {
					ElMessage.success('新增成功');
					emits('handleQuery');
					closeDialog();
				}
				else {
					ElMessage.warning('新增失败：' + res.message);
				}
			});
		}
		//closeDialog();
	});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>